#include <msp430.h>
#include "display.h"
#include "button.h"
#include "init.h"
#include "interrupt.h"
#include "motor.h"
//----------------------------

//display

int num = 0;
char n[4] = {0,0,0,0},pos = 0;
//----------------------------

//buttons
char flag_ButtonPressing = 0;
char flag_ButtonPressed = 0;
char flag_ButtonHolded = 0;
char ButtonPressed = 0;
//----------------------------

//motor
char flag_SpeedChanged = 0;
char speed_level = 0; // 	0 		1 		2 		3 		4
					  //	0   	750		1500	2250	3000
unsigned int speed = 0;
//----------------------------

//time
int sec = 0, min = 0;
char flag_1s = 0;
int count_1ms = 0;
char flag_TimeChanged = 0;
char time_level = 0;
int time_countdown = 0;
//	0	0
//	1	60
//	2	120
//	3	180
//	4	240
//	5	300
//	6	360
//	7	420
//	8	480
//	9	540
// 10	600
char flag_running = 0;
unsigned int test_speed = 500;
//----------------------------

//Over current
unsigned int current = 0;
char over_current = 0;

//#define DEBUG_DISP_TIME

int main(void)
{
  volatile unsigned int j;

  init_osc();
  init_IO();
  init_timer();

//  ADC10CTL0 = ADC10ON + ADC10IE; // ADC10ON, interrupt enabled
//  ADC10CTL1 = INCH_2;                       // input A2
//  ADC10AE0 |= (1<<2);                         // PA.2 ADC option select


  num = 1234;
  DispOneNum(&num);


  for (;;)
  {

	if (flag_ButtonHolded)
	{
		flag_ButtonHolded = 0;

		if (flag_running)
		{
			flag_running = 0;
			P3SEL &= ~(1<<MOTOR);
			motor_off();
		}
		else
		{
			flag_SpeedChanged = 0;
			flag_running = 1;
			P3SEL |= 1<<MOTOR;
			blink_disable();
		}
	}

	if (flag_ButtonPressed)
		{
			flag_ButtonPressed = 0;

			if (isButtonSpeedUpPressed())
			{
				if (flag_running == 0)
				{
					if (speed_level<2)	speed_level++;

				}
				flag_SpeedChanged = 1;
			}
			else if (isButtonSpeedDownPressed())
			{
				if (flag_running == 0)
				{
					if (speed_level>0) speed_level--;

				}
				flag_SpeedChanged = 1;
			}
			else if (isButtonTimerUpPressed())
			{
				if (flag_running == 0)
				{
					if (time_level<10) time_level++;

				}
				flag_TimeChanged = 1;
			}
			else if (isButtonTimerDownPressed())
			{
				if (flag_running == 0)
				{

					if (time_level>0) time_level--;
				}
				flag_TimeChanged = 1;
			}
		}

	if (flag_SpeedChanged)
		{
			flag_SpeedChanged = 0;

			switch (speed_level)
			{
				case 0 :
						 num = 1;
						 speed = 400;
						 break;
				case 1 :
						 num = 2;
						 speed = 600;
						 break;
				case 2 :
						 num = 3;
						 speed = 900;
						 break;
			}
			DispOneNum(&num);
		}

	if (flag_TimeChanged)
	{
		flag_TimeChanged = 0;
		switch (time_level)
		{
			case 0 : time_countdown = 0;
					 break;
			case 1 : time_countdown = 60;
					 break;
			case 2 : time_countdown = 120;
					 break;
			case 3 : time_countdown = 180;
					 break;
			case 4 : time_countdown = 240;
					 break;
			case 5 : time_countdown = 300;
					 break;
			case 6 : time_countdown = 360;
					 break;
			case 7 : time_countdown = 420;
					 break;
			case 8 : time_countdown = 480;
					 break;
			case 9 : time_countdown = 540;
					 break;
			case 10 : time_countdown = 600;
					 break;
		}
		DispOneNum(&time_countdown);
	}



#ifndef DEBUG_DISP_TIME
	if (flag_running)
	{
		if (flag_1s)
		{
			flag_1s = 0;
			if (sec<59)
			{
				sec++;
			}
			else
			{
				sec = 0;
				if (time_countdown>0)
				{
					time_countdown--;
					DispOneNum(&time_countdown);
				}
				else
				{
					flag_running = 0;
				}
			}
		}
		if (current > 30)
		{
			P3SEL &= ~(1<<MOTOR);
			motor_off();
			over_current = 1;
		}
		if ((over_current)&&(current<25))
		{
			P3SEL |= (1<<MOTOR);
		}
	}
	else
	{
		blink_enable();
	}
#endif

#ifdef DEBUG_DISP_TIME
	if (flag_1s)
	{
		flag_1s = 0;

		if (sec<59)
		{
			sec++;
		}
		else
		{
			sec = 0;
			if (min<59)
			{
				min++;
			}
			else min = 0;
		}
		DispTwoNum(&min, &sec);
	}
#endif

  }
}

// ADC10 interrupt service routine
//#pragma vector=ADC10_VECTOR
//__interrupt void ADC10_ISR(void)
//{
//	current = (unsigned int) ADC10MEM;
//	__no_operation();
//}
